package com.yc.common.exceptions.user;

import com.yc.common.constant.HttpStatus;
import com.yc.common.enums.PasswordErrorType;
import com.yc.common.exceptions.ServiceException;
import lombok.Getter;
import lombok.Setter;

/**
 * 密码异常类
 *
 * @Author YanChen
 * @Date 2025/4/24 15:22
 */
@Setter
@Getter
public class PasswordException extends ServiceException {
    private static final long serialVersionUID = 1L;

    /** 密码错误类型枚举 */
    private PasswordErrorType errorType;

    /** 尝试次数 */
    private Integer attempts;

    /** 最大尝试次数 */
    private Integer maxAttempts;

    /** 剩余尝试次数 */
    private Integer remainingAttempts;

    /** 锁定状态 */
    private Boolean locked;

    /** 锁定时间（毫秒） */
    private Long lockTime;

    /** 锁定剩余时间（毫秒） */
    private Long remainingLockTime;

    /** 错误代码 */
    private String errorCode;

    /**
     * 默认构造函数
     */
    public PasswordException() {
        super("密码错误", HttpStatus.BAD_REQUEST);
        this.errorType = PasswordErrorType.INCORRECT;
        this.errorCode = "PASSWORD_ERROR";
    }

    /**
     * 使用自定义消息构造
     *
     * @param message 错误消息
     */
    public PasswordException(String message) {
        super(message, HttpStatus.BAD_REQUEST);
        this.errorType = PasswordErrorType.INCORRECT;
        this.errorCode = "PASSWORD_ERROR";
    }

    /**
     * 使用错误类型构造
     *
     * @param errorType 错误类型
     */
    public PasswordException(PasswordErrorType errorType) {
        super(errorType.getMessage(), HttpStatus.BAD_REQUEST);
        this.errorType = errorType;
        this.errorCode = errorType.getCode();
    }

    /**
     * 使用错误类型构造
     *
     * @param errorType 错误类型
     */
    public PasswordException(PasswordErrorType errorType, String message) {
        super(message, HttpStatus.BAD_REQUEST);
        this.errorType = errorType;
        this.errorCode = errorType.getCode();
    }

    /**
     * 完整构造函数
     *
     * @param message 错误消息
     * @param errorType 错误类型
     * @param attempts 尝试次数
     * @param maxAttempts 最大尝试次数
     * @param locked 是否锁定
     * @param lockTime 锁定时间
     */
    public PasswordException(String message, PasswordErrorType errorType, Integer attempts,
                            Integer maxAttempts, Boolean locked, Long lockTime) {
        super(message, HttpStatus.BAD_REQUEST);
        this.errorType = errorType;
        this.attempts = attempts;
        this.maxAttempts = maxAttempts;
        this.locked = locked;
        this.lockTime = lockTime;
        this.errorCode = errorType.getCode();

        if (attempts != null && maxAttempts != null) {
            this.remainingAttempts = Math.max(0, maxAttempts - attempts);
        }
    }

    /**
     * 带错误代码的构造函数
     *
     * @param message 错误消息
     * @param errorCode 错误代码
     */
    public PasswordException(String message, String errorCode) {
        super(message, HttpStatus.BAD_REQUEST);
        this.errorType = PasswordErrorType.INCORRECT;
        this.errorCode = errorCode;
    }
}